package com.lxzh123;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
//import android.view.animation.Animation;
//import android.view.animation.AnimationUtils;
import android.widget.TextView;

public class ViewFile extends Activity {

	private ScrollTextView title;
	private TextView content;
	
	private int textSize=15;
	private String filenameString;
	
	private SharedPreferences.Editor editor;
	private SharedPreferences preference;
	
	/**
	 * flag=0:打开历史记录
	 * flag=1:打开存储文件
	 */
	private int flag=0;
//	private Animation leftIn;
//	private Animation leftOut;
//	private Animation rightIn;
//	private Animation rightOut;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.filebrowser);

		LxzhApplication.getInstance().addActivity(this);
		
		preference = this.getSharedPreferences("setting",
				NotePad.MODE_WORLD_WRITEABLE);
		
		Log.d("ViewFile", "onCreate");
		title = (ScrollTextView) findViewById(R.id.filepath);
		content = (TextView) findViewById(R.id.view_contents);
		
		Intent intent = getIntent();
		try {
			Uri uri = (Uri) intent.getData();
			filenameString = uri.getPath();
			flag=1;
			Log.d("try", "filenameString=" + filenameString);
		} catch (Exception e) {
			if (e.toString().contains("NullPointerException")) {
				filenameString = intent.getExtras().getString("fileName");
				flag=0;
			}
			Log.d("onCreate", "Exception=" + e.toString());
		}
		textSize=preference.getInt("textSize", 15);
		if(flag==0)
		{
			title.setText(filenameString.substring(filenameString.lastIndexOf("/")+1));
		}else
		{
			title.setText(filenameString);
		}
		String fileContent = EncodingDetect.getFileString(filenameString);
		content.setText(fileContent);
		content.setTextSize(textSize);
	}

//	public void initAnim() {
//		leftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
//		leftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
//		rightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
//		rightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
//	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, 1, 0, R.string.edit).setIcon(R.drawable.ic_edit);
		menu.add(0, 2, 0, R.string.font).setIcon(R.drawable.font);
		menu.add(0, 3, 0, R.string.menu_quit).setIcon(R.drawable.ic_quit);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 1:
			editContent();
			break;
		case 2:
			showDialogFont();
			break;
		case 3:
			LxzhApplication.getInstance().exit();
		default:
			break;
		}
		return super.onOptionsItemSelected( item);
	}

	public void showDialogFont() {
		Log.d("showDialogFont", "1");
		FontButton btnSmall, btnMid, btnBig;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		Log.d("showDialogFont", "2");
		final AlertDialog fontDialog = builder.create();
		fontDialog.show();
		Log.d("showDialogFont", "3");
		Window window = fontDialog.getWindow();
		window.setGravity(Gravity.CENTER);
		WindowManager.LayoutParams lp = window.getAttributes();
		// 设置透明度
		lp.alpha = 0.95f;
		window.setAttributes(lp);
		Log.d("showDialogFont", "4");
		fontDialog.setContentView(R.layout.dialogfont);

		btnSmall = (FontButton) fontDialog.findViewById(R.id.font_small);
		btnMid = (FontButton) fontDialog.findViewById(R.id.font_mid);
		btnBig = (FontButton) fontDialog.findViewById(R.id.font_big);
		btnSmall.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				changeFont(LxzhFont.SMALL);
				fontDialog.cancel();
			}
		});
		btnMid.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				changeFont(LxzhFont.MID);
				fontDialog.cancel();
			}
		});
		btnBig.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				changeFont(LxzhFont.BIG);
				fontDialog.cancel();
			}
		});
	}
	
	public void changeFont(LxzhFont font) {
//		Toast.makeText(getBaseContext(), ""+content.getTextSize(), 3000).show();
		Log.d("changeFont", "1");
		editor = preference.edit();
		switch (font) {
		case SMALL:
			Log.d("changeFont", "2");
			content.setTextSize(15);
			editor.putInt("textSize", 15);
			Log.d("changeFont", "3");
			break;
		case MID:
			Log.d("changeFont", "2");
			content.setTextSize(20);
			editor.putInt("textSize", 20);
			Log.d("changeFont", "3");
			break;
		case BIG:
			Log.d("changeFont", "2");
			content.setTextSize(25);
			editor.putInt("textSize", 25);
			Log.d("changeFont", "3");
			break;
		}
		editor.commit();
		Log.d("changeFont", "4");
	}

	public void editContent() {
		Intent intent = new Intent();
		intent.putExtra("filename", filenameString);
		intent.setClass(ViewFile.this, NotePad.class);
		ViewFile.this.startActivity(intent);
		overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out);
		// MyApplication.getInstance().exit();
		/*
		 * Bundle bundle=new Bundle();
		 * bundle.putString("filename",filenameString); SharedPreferences
		 * preferences = getSharedPreferences("Text", 0);
		 * SharedPreferences.Editor editor = preferences.edit();
		 * editor.putString("title", filenameString);
		 * setResult(Activity.RESULT_OK,this.getIntent().putExtras(bundle));
		 * Intent in = new Intent(ViewFile.this, NotePad.class);
		 * in.putExtras(bundle); startActivityForResult(in, 0);
		 */
	}
	
	public void changeSizeCode(int size)
	{
		
	}

	private enum LxzhFont {
		SMALL, MID, BIG
	};
}
